package com.zhang.gulimall.cart.web;

import com.zhang.gulimall.cart.service.CartService;
import com.zhang.gulimall.cart.vo.Cart;
import com.zhang.gulimall.cart.vo.CartItem;
import com.zhang.gulimall.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CartController {

    @Autowired
    CartService cartService;

    // 展示购物车界面
    @GetMapping("cartList.html")
    public String cartListPage(Model model, HttpSession session, HttpServletRequest request) {
        Cart cart = cartService.getCart(session, request);
        cart.setTotalAmount(cartService.countTotal(cart.getItems()));
        model.addAttribute("cart", cart);
        return "cartList";
    }

    // 添加购物车成功界面
    @GetMapping("success.html")
    public String successPage(Model model) {
        return "success";
    }

    // 添加购物车动作
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") String skuId,
                            @RequestParam("num") Integer num,
                            Model model){
        CartItem cartItem = cartService.addToCart(skuId, num);
        model.addAttribute("cartItem", cartItem);
        return "success";
    }
    // 修改勾选状态
    @GetMapping("/changeCheck")
    public String changeCheck(@RequestParam("isChecked") Boolean isChecked,
                            @RequestParam("skuId") String skuId,
                            Model model, HttpSession session, HttpServletRequest request){
        Cart cart = cartService.getCart(session, request);
        cart = cartService.changeCheck(cart, isChecked, skuId);
        model.addAttribute("cart", cart);
        return "redirect:http://localhost:16000/cartList.html";
    }
    // 修改数量
    @GetMapping("/changeNum")
    public String changeNum(@RequestParam("num") Integer num,
                            @RequestParam("skuId") String skuId,
                            Model model, HttpSession session, HttpServletRequest request){
        Cart cart = cartService.getCart(session, request);
        cart = cartService.changeNum(cart, num, skuId);
        model.addAttribute("cart", cart);
        return "redirect:http://localhost:16000/cartList.html";
    }
    // 删除商品
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") String skuId,
                             Model model, HttpSession session, HttpServletRequest request){
        Cart cart = cartService.getCart(session, request);
        cart = cartService.deleteItem(cart, skuId);
        model.addAttribute("cart", cart);
        return "redirect:http://localhost:16000/cartList.html";
    }
    // 获取购物项
    @RequestMapping("/usercartitems")
    @ResponseBody
    public R getUserCartItems(){
        List<CartItem> userCartItems = cartService.getUserCartItems();
        return R.ok().put("cartItems",userCartItems);
    }
}
